package test3;
import java.util.ArrayList;
import java.util.Scanner;
public class test3 {

    public static void main(String[] args) {

        System.out.println("初始化栈:\n请输入初始栈的大小N: ");

        Scanner scanner = new Scanner(System.in);

        int N = scanner.nextInt();

        ArrayList<Integer> stack = new ArrayList<>(N);



        if(N!=0)

        {



            System.out.println("请依次输入" + N + "个整数用于初始化栈: ");

            for (int i = 0; i < N; i++) {

                int num = scanner.nextInt();

                stack.add(num);

            }



        }

        System.out.println("入栈:\n请输入要入栈的整数: ");

        int numToPush = scanner.nextInt();

        stack.add(numToPush);

        System.out.println("元素已入栈");



        if (stack.isEmpty()) {



            System.out.println("出栈:\n栈为空:无法出栈");



        }

        else {

            int poppedElement = stack.remove(stack.size() - 1);

            System.out.println("出栈:\n出栈元素: " + poppedElement);



        }



        if (stack.isEmpty()) {

            System.out.println("查看栈顶元素:\n栈为空");

        }

        else {



            int peekedElement = stack.get(stack.size() - 1);

            System.out.println("查看栈顶元素:\n栈顶元素: " + peekedElement);

        }

    }

}